package com.xyz.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xyz.entity.Announcement;

import com.xyz.entity.vo.ListAnnouncementVO;
import com.xyz.entity.request.PublishAnnouncementRequest;
import com.xyz.mapper.AnnouncementMapper;
import com.xyz.service.IAnnouncementService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 公告表 服务实现类
 * </p>
 *
 * @author author
 * @since 2022-04-27
 */
@Service
public class AnnouncementServiceImpl extends ServiceImpl<AnnouncementMapper, Announcement> implements IAnnouncementService {
    @Override
    public Page<ListAnnouncementVO> listAnnouncement(Integer page, Integer limit) {
        Page<Announcement> page1 = this.page(new Page<>(page, limit));
        List<Announcement> list = page1.getRecords();
        List<ListAnnouncementVO> collect = list.stream().map(o -> {
            ListAnnouncementVO item = new ListAnnouncementVO();
            BeanUtils.copyProperties(o, item);
            return item;
        }).collect(Collectors.toList());
        Page<ListAnnouncementVO> listPage = new Page<>();
        BeanUtils.copyProperties(page1, listPage);
        listPage.setRecords(collect);
        return listPage;
    }

    @Override
    public void publishAnnouncement(PublishAnnouncementRequest request) {
        this.save(new Announcement(request.getContent()));
    }
}
